package com.shouhe.mysap.controller.sale;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.SelectGroupResult;
import com.shouhe.mysap.entity.PurPerson;
import com.shouhe.mysap.entity.SalePerson;
import com.shouhe.mysap.service.SalePersonService;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 销售人员控制器
 * sale_person表
 * </p>
 * @author 守鹤
 * @since 2020-05-31
 */
@RestController
@RequestMapping("/sale-person")
public class SalePersonController {

    @Autowired
    private SalePersonService salePersonService;

    /**
     * 分页查询全部的销售人员
     * @param page 前端传递的页码
     * @param rows 前端传递的显示条数
     * @param personGroup 销售组
     * @param personName  销售人员名字，用来模糊查询
     * @return  分页数据
     */
    @RequestMapping("/getSalePersons")
    public PageResult getSalePersons(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,@RequestParam(defaultValue = "-1") Integer personGroup,@RequestParam(defaultValue = "null")String personName) {
        Map<String, Object> eqMap = new HashMap<>();
        Map<String, Object> likeMap = new HashMap<>();
        eqMap.put("sale_group",personGroup);
        likeMap.put("sale_name",personName);
        PageResult pageResult = PageResultUtil.getPageResultEqLikeMap(page, rows, salePersonService,eqMap,likeMap);
        return pageResult;
    }

    /**
     * @描述 通过销售组得到销售人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getSalePersonBySaleGroup")
    public SalePerson getSalePersonBySaleGroup(Integer saleGroup) {
        SalePerson salePerson = SelectUtil.selectOneEq("sale_group", saleGroup, salePersonService);
        return salePerson;
    }

    /**
     * @描述 通过名字得到销售人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getSalePersonBySaleName")
    public SalePerson getSalePersonBySaleName(String saleName) {
        SalePerson salePerson = SelectUtil.selectOneEq("sale_name", saleName, salePersonService);
        return salePerson;
    }

    /**
     * 得到全部的销售组,用来填充下拉框
     * @param saleGroup  前端传递过来的销售组，用来做默认的填充
     * @param selected 是否需要默认填充一个
     * @return 全部销售组
     */
    @RequestMapping("/getSaleGroupResults")
    public List<SelectGroupResult> getSaleGroups(@RequestParam(defaultValue = "-1") Integer saleGroup,@RequestParam(defaultValue = "true") boolean selected) {
        List<SelectGroupResult> selectGroupResults = new ArrayList<>();
        List<SalePerson> list = salePersonService.list(null);
        for (SalePerson person : list) {
            SelectGroupResult result = new SelectGroupResult();
            result.setContent(person.getSaleGroup() + "组");
            result.setValue(person.getSaleGroup() + "");
            //如果有指定需要哪一个组，则选择那一组
            if(person.getSaleGroup()==saleGroup){
                result.setSelected(true);
            }
            selectGroupResults.add(result);
        }
        if (selectGroupResults.size() > 0) {
            //如果没有指定需要哪一个组，并且需要选择一组，则默认选择第一个组
            if(saleGroup==-1&&selected){
                selectGroupResults.get(0).setSelected(true);
            }
        }
        return selectGroupResults;
    }


    /**
     * 添加一个销售员
     * @param salePerson  销售员的对象
     * @return 前端接收的执行结果对象
     */
    @RequestMapping("/addSalePerson")
    public MySapResult addSalePerson(SalePerson salePerson) {
        //首先判断销售组是否已经存在
        SalePerson person = getSalePersonBySaleGroup(salePerson.getSaleGroup());
        if (person != null) {
            return MySapResult.fail("添加销售员失败,销售组已经存在");
        }
        return AddUtil.addObject("添加销售员",salePerson,salePersonService);
    }



    /**
     * 通过id更新一个销售员
     * @param person  销售员信息
     * @return  前端接收的执行结果对象
     */
    @RequestMapping("/updateSalePerson")
    public MySapResult updateSalePerson(SalePerson person,@RequestParam(defaultValue = "false") boolean updateGroup) {
        if(updateGroup){
            SalePerson sale = getSalePersonBySaleGroup(person.getSaleGroup());
            if(sale!=null){
                return MySapResult.fail("销售组已经存在，更新信息失败");
            }
            return UpdateUtil.updateObjectById("更新销售员信息",person,salePersonService);
        }else{
            person.setSaleGroup(null);
            return UpdateUtil.updateObjectById("更新销售员信息",person,salePersonService);
        }
    }


    /**
     *通过 id 删除销售员，逻辑上的删除
     * @param ids 销售员id的数组
     * @return 前端接收的执行结果对象
     */
    @RequestMapping("/deleteSalePersons")
    public MySapResult deleteSalePersons(@RequestParam("ids[]") Integer[] ids) {
        return  DeleteUtil.deleteMany("删除销售员",ids,salePersonService);
    }


}

